<?php

namespace app\batch;

use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\facade\Debug;
use app\models\{Goods, GoodsSpec, SystemGoods, SystemGoodsSpec, Business, Sucai, OperLog};
use think\Db;
use think\facade\Log;
use app\facade\Redis;

class Rebot extends Command
{
    protected $open = false;
    protected function configure()
    {
        $this->setName('Rebot')->setDescription('测试');
    }

    protected function execute(Input $input, Output $output)
    {
        $this->doCron();
        die();
    }

    protected function doCron()
    {
        ini_set('max_execution_time', 0);
        (new Business())->where(['schedule' => [2,3, 4, 5, 6, 7]])->chunk(5, function ($list) {
            foreach ($list as $row) {
                $row['images3'] = json_decode($row['images3'], true) ?? [];
                $row['images4'] = json_decode($row['images4'], true) ?? [];
                $row['images10'] = json_decode($row['images10'], true) ?? [];
                $row['video'] = json_decode($row['video'], true) ?? [];
                $row['video1'] = json_decode($row['video1'], true) ?? [];
                //选址素材查询
                if (count($row['images4']) > 0 || count($row['images10']) > 0 || count($row['video1']) > 0) {
                    $ishave = (new Sucai())->where(['isdefault' => 0, 'orderid' => $row['id'], 'type' => 1])->find();
                    $oper = (new OperLog())->where(['type' => 2, 'sonmenu' => ['addxuanzhi_new', 'addxuanzhi'], 'remark' => $row['id']])->value('created');
                    $xuan = [
                        'isdefault' => 0,
                        'user_id' => 0,
                        'orderid' => $row['id'],
                        'type' => 1,
                        'pinpai' => $row['type'],
                        'images4' => json_encode($row['images4'], true) ?? '',
                        'images10' => json_encode($row['images10'], true) ?? '',
                        'video1' => json_encode($row['video1'], true) ?? '',
                        'sctype' => 3,
                        'status' => 2,
                        'finished' => $oper ?? $row['xtimes']
                    ];
                    if (empty($ishave)) {
                        (new Sucai())->allowfield(true)->save($xuan);
                    } else {
                        (new Sucai())->allowfield(true)->save($xuan, ['id' => $ishave['id']]);
                    }
                }
                //开业素材
                if (count($row['images3']) > 0 ||  count($row['video']) > 0) {
                    $ishave = (new Sucai())->where(['isdefault' => 0, 'orderid' => $row['id'], 'type' => 2])->find();
                    $oper = (new OperLog())->where(['type' => 2, 'sonmenu' => ['addkaiye'], 'remark' => $row['id']])->value('created');
                    $kai = [
                        'isdefault' => 0,
                        'user_id' => 0,
                        'orderid' => $row['id'],
                        'type' => 2,
                        'pinpai' => $row['type'],
                        'images3' => json_encode($row['images3'], true) ?? '',
                        'video' => json_encode($row['video'], true) ?? '',
                        'sctype' => 3,
                        'status' => 2,
                        'finished' => $oper ?? $row['xtimes']
                    ];
                    if (empty($ishave)) {
                        (new Sucai())->allowfield(true)->save($kai);
                    } else {
                        (new Sucai())->allowfield(true)->save($kai, ['id' => $ishave['id']]);
                    }
                }
            }
        });
        // (new SystemGoods())->chunk(1, function ($lists) {
        //     foreach ($lists as $list) {
        //         $frtimes = Redis::get('orderRefund' . $list['id']);
        //         if ($frtimes) {
        //             continue;
        //         } else {
        //             Redis::setex('orderRefund' . $list['id'], 10, 10);
        //         }
        //         if ($list['classid'] == 154) {
        //             $list['classid'] = 2;
        //         }
        //         if ($list['classid'] == 155) {
        //             $list['classid'] = 9;
        //         }
        //         if ($list['classid'] == 156) {
        //             $list['classid'] = 5;
        //         }
        //         if ($list['classid'] == 157) {
        //             $list['classid'] = 7;
        //         }
        //         if ($list['classid'] == 158) {
        //             $list['classid'] = 11;
        //         }
        //         if ($list['classid'] == 159) {
        //             $list['classid'] = 6;
        //         }
        //         if ($list['classid'] == 160) {
        //             $list['classid'] = 12;
        //         }
        //         if ($list['classid'] == 161) {
        //             $list['classid'] = 4;
        //         }
        //         if ($list['classid'] == 162) {
        //             $list['classid'] = 8;
        //         }
        //         if ($list['classid'] == 163) {
        //             $list['classid'] = 10;
        //         }
        //         if ($list['classid'] == 164) {
        //             $list['classid'] = 13;
        //         }
        //         if ($list['classid'] == 165) {
        //             $list['classid'] = 14;
        //         }
        //         if ($list['classid'] == 166) {
        //             $list['classid'] = 1;
        //         }
        //         if ($list['classid'] == 167) {
        //             $list['classid'] = 3;
        //         }
        //         (new SystemGoods())->where('id', $list['id'])->update(['classid' => $list['classid']]);
        //         // $spec = (new GoodsSpec())->where('goodid', $list['id'])->find();
        //         // if (!empty($spec)) {
        //         //     $goods = [
        //         //         'classid' => $list['cate'],
        //         //         'title' => $list['title'],
        //         //         'images' => $list['images'],
        //         //         'recomm' => $list['isHot'],
        //         //         'isnew' => $list['isNew'],
        //         //         'keywords' => $list['Keywords'],
        //         //         'descs' => $list['descs'],
        //         //         'goodsDesc' => $list['goodsDesc']
        //         //     ];
        //         //     $goodspec = [
        //         //         'sales' => $list['sale'],
        //         //         'goodid' => 0,
        //         //         'name' => $spec['name'],
        //         //         'price' => $spec['price'],
        //         //         'unmit' => ''
        //         //     ];
        //         //     Db::startTrans();
        //         //     try {
        //         //         $goodid = (new SystemGoods())->insertGetId($goods);
        //         //         $goodspec['goodid'] = $goodid;
        //         //         (new SystemGoodsSpec())->allowfield(true)->save($goodspec);
        //         //         Db::commit();
        //         //     } catch (\Exception $e) {
        //         //         dump($e->getMessage());
        //         //         Db::rollback();
        //         //     }
        //         // }
        //     }
        // });
    }
}
